/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2025-04-12
 * Time: 20:54
 */
public class Demo24 {
    private static int count;

    //synchronized 修饰非静态方法
    synchronized public void add() {
        count++;
    }

    public static void main(String[] args) throws InterruptedException {
        Demo24 demo  =  new Demo24();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                demo.add();
            }
            System.out.println("t1线程结束");
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                demo.add();
            }
            System.out.println("t2线程结束");
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(count);
    }
}
